<?php
namespace app\common\exception;

use Exception;
use think\exception\ErrorException;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;
use think\facade\Env;

class MyExceptionHandler extends Handle
{
    //重写Handle的render方法，实现自定义异常消息
    public function render(Exception $e)
    {
        //找不到对应的数据
        if ($e instanceof EmptyException) {
            return json(array('code' => $e->code, 'msg' => $e->getMessage(), 'message' => $e->message), $e->httpCode);
        }

        if ($e instanceof ValidateException) {
            return json(array('code' => 422, 'msg' => $e->getMessage(), 'message' => $e->getMessage()), 200);
        }

        if ($e instanceof ForbidenException) {
            return json(array('code' => 403, 'msg' => $e->getMessage(), 'message' => $e->getMessage()), 200);
        }

        //对于thinkphp的报错处理 500的异常处理
        if ($e instanceof HttpException || $e instanceof ErrorException) {
            // if ($e->getMessage() == '该渠道已关闭') {
            //     return redirect(url('/errors', array('msg' => $e->getMessage())));
            // } else {
            //       return redirect(url('/errors', array('msg' => $e->getMessage())));
            // }

        }



        //开发者对异常的操作

        return parent::render($e);
    }
}